package D2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:程序命名为Reflect学号.java，在其中完成以下功能。
 * (1)使用反射获得实验2中的第6题中Rectangle类的全部构造方法、全部属性、全部方法、实现的接口和继承的类。
 * (2)使用反射创建一个Rectangle类的对象,求出该对象的面积
 * User: 86183
 */
public class Reflect220054070111 {
    public static void main(String[] args) throws  ClassNotFoundException,InstantiationException,IllegalAccessException,
            NoSuchMethodException, InvocationTargetException {
        Class<?> cls = Class.forName("D2.Rectangle");
        Rectangle o = (Rectangle) cls.newInstance();
        System.out.println(o);
        o.setHeight(2);
        o.setWidth(3);
        System.out.println(o);
        Constructor<?> c1 = cls.getDeclaredConstructor();
        Constructor<?> c2 = cls.getDeclaredConstructor(double.class,double.class);
        Object o1 = c2.newInstance(1,2);
        System.out.println(o1);

    }
    public static void main1(String[] args) throws ClassNotFoundException {
        Class< ? > cls = Class.forName("D2.Rectangle");
        Constructor<?>[] dConstructorS = cls.getDeclaredConstructors();
        System.out.println("全部构造方法");
        for(Constructor<?>d:dConstructorS){
            System.out.println(d);
        }
        Field[] declaredFields = cls.getDeclaredFields();
        System.out.println("全部属性");
        for (int i = 0; i < declaredFields.length; i++) {
            System.out.println(declaredFields);
        }
        Method[] declareMethods = cls.getMethods();
        System.out.println("全部方法");
        for(Method declaredMethod : declareMethods){
            System.out.println(declaredMethod);
        }
        Class<?> superclass = cls.getSuperclass();
        System.out.println("父类是" + superclass);
    }
}
